package com.yb.bussiness.global;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.member.TsSysuser;
import com.yb.domain.system.SystemLog;
import com.yb.util.comm.Contants;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.PageUtil;

public class SystemLogServiceImpl implements SystemLogService {

	private IBaseDao<SystemLog, Long> dao;

	public SystemLogServiceImpl() {
		dao = new BaseDao<SystemLog, Long>(SystemLog.class);
	}

	public PageUtil getList(Map<String, String> map, int start, int limit) {
		if (map == null || map.size() == 0)
		return null;
		PageUtil pu = new PageUtil();
		String find_sql ="SELECT * FROM t_s_system_log where 1=1 ";
		String countSql ="SELECT count(*) FROM t_s_system_log where 1=1 ";
		try {
			if (!"".equals(map.get("startTime"))){
				if(!"".equals(map.get("endTime"))){
					find_sql += "  AND  optTime BETWEEN '"+map.get("startTime")+"' AND '"+map.get("endTime")+"'";
					countSql += "  AND  optTime BETWEEN '"+map.get("startTime")+"' AND '"+map.get("endTime")+"'";
				}else{
					find_sql += "  and optTime>='"+map.get("startTime")+"'";
					countSql += "  and optTime>='"+map.get("startTime")+"'";
				}
			}else if(!"".equals(map.get("endTime"))){
				find_sql += "  and optTime<='"+map.get("endTime")+"'";
				countSql += "  and optTime<='"+map.get("endTime")+"'";
			}
			if (null != map.get("title") && !"".equals(map.get("title"))) {
				find_sql += "  and title like '%" + map.get("title") + "%'";
				countSql += "  and title like '%" + map.get("title") + "%'";
			}
			
			List<Object[]> countList = dao.findObjectArryList(countSql.toString());
			List<SystemLog> newlist = null;
			if (null != countList && countList.size() > 0) {
				Object[] countAry = countList.get(0);
				pu.setTotalRecords(new Integer(countAry[0].toString()));
				List<SystemLog> list = dao.findList(find_sql+" ORDER BY optTime desc limit ?,?",new Object[]{(start-1)*limit,limit});
				if (list != null && list.size() > 0) {
					newlist = new ArrayList<SystemLog>();
					for (SystemLog tp : list) {
						tp.setOptTimeString(DateTimeUtil.switchDateToString(tp.getOptTime(), "yyyy-MM-dd HH:mm:ss"));
						newlist.add(tp);
					}
				}
			} else {
				pu.setTotalRecords(0);
			}
			if (null == newlist)
				newlist = new ArrayList<SystemLog>();
			pu.setList(newlist);
			pu.setPageNo(start);
			pu.setPageSize(limit);
			return pu;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		}

	public boolean deleteByIdString(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from t_s_system_log where id=" + delteid[i];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public void saveSystemLog(HttpServletRequest request, String title,
			String content) {
		TsSysuser tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(request, Contants.SESSION_USER);
		SystemLog obj = new SystemLog();
		obj.setTitle(title);
		obj.setContent(content);
		obj.setIpAddress(getIpAddr(request));
		obj.setOptTime(new Date());
		obj.setUserId(tsSysuser.getId());
		obj.setUserName(tsSysuser.getUsername());
		obj.setUserType(tsSysuser.getType());
		dao.saveEntity(obj);
	}
	
	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
